package oocis.information;

import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import oocis.util.*;
import oocis.plat.*;
import database.*;

public class InfoDistributor {
	private static Distributor distributor;

	private static Storage storage = new Storage();

	private static DBManager dbManager = new DBManager();

	private static String dbName = "InfomationDatabase";

	private static String[] dbColName = { "ID", "owner", "startTime", "endTime" };

	private static int[] dbColLen = { 20, 20, 50, 50 };

	private static int infoId = 0, infoOwner = 1, infoStartTime = 2,
			infoEndTime = 3;

	public static void initialize() {
		dbManager.initialize();
		dbManager.creat(dbName, dbColName, dbColLen);
	}

	public static void finalization() {
		dbManager.finalize();
	}

	public static int allInfoNum() {
		initialize();

		int size = dbManager.getAll().size();

		finalization();

		return size;
	}

	private static void debug(Vector<String[]> abc) {
		for (int i = 0; i < abc.size(); i++) {
			for (int j = 0; j < abc.get(i).length; j++)
				System.out.print(abc.get(i)[j] + " ");
			System.out.println();
		}
	}

	public static void addInfo(Information i) {
		initialize();

		String[] val = { "" + i.infoId, i.owner,
				"" + i.timeLine.getStartTime().incode(),
				"" + i.timeLine.getEndTime().incode() };
		dbManager.add(dbColName, val);

		storage.infoStore(i);

		finalization();
	}

	public static Information getInfo(int i) {
		initialize();

		String[] getName = { "owner", "startTime", "endTime" };

		Vector<String[]> tmp = dbManager.query(getName, dbColName[infoId], ""
				+ i);
		if (tmp == null)
			return null;
		String[] v = tmp.get(0);

		Information newInfo = new Information(i, v[0], new InfoText(),
				new TimeInter(new Time(v[1]), new Time(v[2])));
		storage.infoLoad(newInfo);

		finalization();

		return newInfo;
	}

	public static Vector<Information> active(Time tm) {
		initialize();
		long code = tm.incode();
		String[] getName = { dbColName[infoId] };
		String noLessName = dbColName[infoStartTime];
		String noGreatName = dbColName[infoEndTime];
		String value = "" + code;
		Vector<String[]> allId = dbManager.queryInter2(getName, noLessName,
				noGreatName, value);

		debug(allId);
		
		Vector<Information> vi=new Vector<Information>();

		for (int i = 0; i < allId.size(); i++) {
			Information info = getInfo(Integer.valueOf((allId.get(i)[0])));
			vi.add(info);

			/*
			 * Do something to distributor it;
			 */
		}

		finalization();
		
		return vi;
	}

	public static void main(String[] args) {
		
//		Time tt=new Time();
//		System.out.println(tt.getMonth());
//		System.out.println(tt.incode());
//		
//		Information i = new Information(1, "abc", new InfoText("head", "body",
//				"abstract"), new TimeInter());
//		Information i2 = new Information(2, "abc", new InfoText("head", "body",
//				"abstract"), new TimeInter(new Time(new Long(123)), new Time(new Long(345))));
//		addInfo(i);
//		addInfo(i2);
//
//		active(new Time(new Long(150)));
//
//		Information ii = getInfo(2);
//		System.out.println(ii.info.infoHead);
	}

}
